<?php

class Image extends Controller {

    public function __construct() {
        parent::__construct();
        Session::init();
        $logged = Session::get('loggedIn');
        $role = Session::get('role');

        if ($logged == false || $role != 'owner') {
            Session::destroy();
            header('location: ' . URL . 'login');
            exit;
        }
    }

    public function index() {
        header('location: ' . URL);
    }

    public function save($idNews) {
        $upload = new Upload();
        $dir = __DIR__ . '/../../web/media/images/news/test/' . $idNews . '/';
        if (!is_dir($dir)) {
            mkdir($dir);
        }
        chmod($dir, 0755);
        $upload->_dir = $dir;
        $upload->_key = 'file';
        $run = $upload->run();
        if ($run != FALSE) {

            /** Tamaño de 100x100 * */
            $dir = __DIR__ . '/../../web/media/images/news/test/' . $idNews . '/100/';
            if (!is_dir($dir)) {
                mkdir($dir);
            }
            chmod($dir, 0755);
            $imgUpload = new Img();
            $imgUpload->resize($run['dir'] . $run['filename'], 100, 100, $dir . $run['filename']);

            /** Tamaño de 200x200 * */
            $dir = __DIR__ . '/../../web/media/images/news/test/' . $idNews . '/200/';
            if (!is_dir($dir)) {
                mkdir($dir);
            }
            chmod($dir, 0777);
            $imgUpload->resize($run['dir'] . $run['filename'], 200, 200, $dir . $run['filename']);
        }
        $data['messageAction'] = 'Imagenes cargadas con exito.';
        $this->model->saveImage($run['filename'], $idNews);
        print "<script>parent.resultSubmitImages(" . json_encode($data) . "); </script>";
//        $this->view->render('news/images_form', true);
    }

    public function deleteImg($idImg) {
        
    }

}